// owner-app/pages/form-subscribe-new/index.js
import { handlerGobackClick, hint, checkIDCard } from "../../../utils/util";
const app = getApp();
import Apipublic from "../../../models/api";
const apipublic = new Apipublic();
import { Subscribe } from "../../../models/subscribe";
const subscribe = new Subscribe();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    name: "",
    idCard: "",
    phone: "",
    relationshipName: "",
    visitorValidityName: "",
    smallRemarkName: "",
    houseId: "",
    myAddress: "",

    relationshipShow: false,
    visitorValidityShow: false,
    smallRemarkShow: false,
    myAddressShow: false,
    columnsRelationship: [],
    columnsVisitorValidity: [],
    columnsSmallRemark: [],
    columnsMyAddress: [],

    paiNew: wx.getStorageSync("paiNew"),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    this.classify();
  },
  handlerGobackClick,
  // 控制选择框显示隐藏
  handleShowPicker(e) {
    var showflag = e.currentTarget.dataset.showflag;
    var { showname } = e.currentTarget.dataset;
    if (!showflag) {
      this.setData({
        [showname]: false,
      });
      return false;
    }

    this.setData({
      [showname]: JSON.parse(e.currentTarget.dataset.showflag),
    });
  },
  relationshipConfirm(e) {
    this.setData({
      relationship: e.detail.value.value,
      relationshipName: e.detail.value.text,
      relationshipShow: false,
    });
  },
  columnsVisitorConfirm(e) {
    this.setData({
      visitorValidity: e.detail.value.value,
      visitorValidityName: e.detail.value.text,
      visitorValidityShow: false,
    });
  },
  smallRemarkConfirm(e) {
    this.setData({
      smallRemark: e.detail.value.value,
      smallRemarkName: e.detail.value.text,
      smallRemarkShow: false,
    });
  },
  myAddressConfirm(e) {
    this.setData({
      houseId: e.detail.value.id,
      myAddress: e.detail.value.myAddress,
      myAddressShow: false,
    });
  },
  onChange(e) {
    let key = e.currentTarget.dataset.key;
    let value = e.detail;
    this.setData({
      [key]: value,
    });
  },
  idCardNumber() {
    let idCard = checkIDCard(this.data.idCard);
    if (!idCard) {
      this.setData({
        idCard: "",
      });
      hint("请输入正确证件号码");
      return;
    }
  },
  submitLastAll() {
    if (!this.data.name) {
      hint("请输入姓名");
      return;
    }

    if (!this.data.idCard) {
      hint("请输入身份证号");
      return;
    }

    if (!this.data.phone) {
      hint("请输入联系电话");
      return;
    }

    if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.data.phone)) {
      hint("联系电话有误，请重填");
      return;
    }

    if (!this.data.relationshipName) {
      hint("请选择与我关系");
      return;
    }

    if (!this.data.visitorValidityName) {
      hint("请选择门禁有效期");
      return;
    }

    if (!this.data.smallRemarkName) {
      hint("请选择到访事由");
      return;
    }

    if (!this.data.myAddress) {
      hint("请选择我的地址");
      return;
    }

    let data = {
      name: this.data.name,
      idCard: this.data.idCard,
      phone: this.data.phone,
      relationship: this.data.relationship,
      visitorValidity: this.data.visitorValidity,
      smallRemark: this.data.smallRemark,
      myAddress: this.data.myAddress,
      houseId: this.data.houseId,
      inviterId: wx.getStorageSync("user").memberId,
      inviterPhone: wx.getStorageSync("user").phone,
      createBy: wx.getStorageSync("user").username,
    };

    subscribe.miniVisitorAdd(data).then((res) => {
      if (res.code == 1000) {
        hint("邀约成功");
        wx.navigateBack();
      }
    });
  },
  classify() {
    //与我关系类型
    apipublic.dictItemList("small_visitor_relation").then((res) => {
      this.setData({
        columnsRelationship: res.result,
      });
    });

    //门禁有效期类型
    apipublic.dictItemList("visitor_validity").then((res) => {
      this.setData({
        columnsVisitorValidity: res.result,
      });
    });

    // 到访事由类型
    apipublic.dictItemList("small_remark").then((res) => {
      this.setData({
        columnsSmallRemark: res.result,
      });
    });

    // 我的地址类型
    subscribe
      .miniVisitorGetAddress({ id: wx.getStorageSync("user").memberId })
      .then((res) => {
        this.setData({
          columnsMyAddress: res.result,
        });
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
